package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.JournalEntry.QuarterlyJournal;
import com.gljx.web.Entity.User;
import com.gljx.web.service.QuarterlyJournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/")
public class QuarterlyJournalController {
	
	
	@Autowired
	private QuarterlyJournalService quarterlyJournalService;
	
	@RequestMapping(value="queryQuarterlyJournal/look.action")
	public String selQuarterlyJournal(HttpServletRequest request,String year,String quarter)throws Exception{
		//获取当前操作人
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		String [] months= new String[3];//月份
		months[0]="01";
		months[1]="02";
		months[2]="03";
		if(quarter == null ||quarter.equals("")){
			quarter="一";
		}
		if(year == null || year.equals("")){
			SimpleDateFormat df = new SimpleDateFormat("yyyy");//设置日期格式
			year=df.format(new Date());
		}
		if(quarter !=null && !quarter.equals("")){
			if(quarter.equals("一")){
				months[0]="01";
				months[1]="02";
				months[2]="03";
			}else if(quarter.equals("二")){
				months[0]="04";
				months[1]="05";
				months[2]="06";
			}else if(quarter.equals("三")){
				months[0]="07";
				months[1]="08";
				months[2]="09";
			}else {
				months[0]="10";
				months[1]="11";
				months[2]="12";
			}
		}
		//分3次去取数据
		List<QuarterlyJournal> quarterlyJournalList1= quarterlyJournalService.selQuarterlyJournal(user.getFcompanyid(),year+months[0]);
		List<QuarterlyJournal> quarterlyJournalList2= quarterlyJournalService.selQuarterlyJournal(user.getFcompanyid(), year+months[1]);
		List<QuarterlyJournal> quarterlyJournalList3= quarterlyJournalService.selQuarterlyJournal(user.getFcompanyid(), year+months[2]);
		List<QuarterlyJournal> quarterlyJournalList=new ArrayList<>();
		
		//把3次的数据拼接成一个实体类里面
		for (int i = 0; i < quarterlyJournalList1.size(); i++) {
			//System.out.println(quarterlyJournalList1.get(i).getName2()+ "  "+quarterlyJournalList2.get(i).getName2()+" "+quarterlyJournalList3.get(i).getName2() );
			QuarterlyJournal _QuarterlyJournal=new QuarterlyJournal();
			_QuarterlyJournal.setName2(quarterlyJournalList1.get(i).getName2());
			_QuarterlyJournal.setCfmount_1(quarterlyJournalList1.get(i).getCfmount2());
			for (int j = 0; j < quarterlyJournalList2.size(); j++) {
				if(quarterlyJournalList1.get(i).getName2().equals(quarterlyJournalList2.get(j).getName2())){
					_QuarterlyJournal.setCfmount_2(quarterlyJournalList2.get(j).getCfmount2());
					break;
				}
			}
			for (int k = 0; k < quarterlyJournalList3.size(); k++) {
				if(quarterlyJournalList1.get(i).getName2().equals(quarterlyJournalList3.get(k).getName2())){
					_QuarterlyJournal.setCfmount_3(quarterlyJournalList3.get(k).getCfmount2());
					break;
				}				
			}
			quarterlyJournalList.add(_QuarterlyJournal);
		}
		/*for (QuarterlyJournal quarterlyJournal : quarterlyJournalList) {
			System.out.println("项目: "+quarterlyJournal.getName2()+" 第一个月:"+quarterlyJournal.getCfmount_1()+" 第二个月: "+quarterlyJournal.getCfmount_2()+"第三个月: "+quarterlyJournal.getCfmount_3()
			+" 季度总额:"+quarterlyJournal.getSumQuarterly());
		}*/
		request.setAttribute("quarterlyJournalList", quarterlyJournalList);
		request.setAttribute("year", year);
		request.setAttribute("quarter", quarter);
		return"backHome/journaling/manageJournaling/quarterlyJournaling/quarterlyJournalingList";
	}
}
